package com.sxz.mall.mallcart.controller;

import com.alibaba.fastjson.JSON;
import com.sxz.mall.common.constant.AuthConstant;
import com.sxz.mall.common.vo.MemberVO;
import com.sxz.mall.mallcart.interceptor.AuthInterceptor;
import com.sxz.mall.mallcart.service.ICartService;
import com.sxz.mall.mallcart.vo.Cart;
import com.sxz.mall.mallcart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    // 不写修饰符，默认default   包级访问

    @Autowired
    ICartService cartService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/getUserCartItems")
    @ResponseBody
    public List<CartItem> getUserCartItems() {
        System.out.println("获取用户对应的购物车列表信息");
        return cartService.getUserCartItems();
    }

    @GetMapping("/cart_list")
    public String queryCartList(Model model) {

        Cart cart = cartService.getCartList();

        model.addAttribute("cart", cart);

        return "cartList";
    }

    ;

    @GetMapping("/addCart")
    public String addCart(Long skuId, Integer num, Model model) {
        System.out.println("添加购物车成功....skuId:" + skuId + "数量：" + num);

        // 把商品添加到购物车中,    数据传入html
        CartItem item = null;
        try {
            item = cartService.addCart(skuId, num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("item", item);
        System.out.println("获取商品信息，加入购物车页面展示" + item);
        return "success";
    }
}
